﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClickerClimber.Scenes.SplashScreenScene;
using ClickerClimber.Scenes;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using ClickerClimber.Model;

namespace ClickerClimber.Managers
{
    class AnimationManager
    {
        private static AnimationManager _instance;

        Vector2 _pos;
        List<Texture2D> _beaver_bite;
        List<Texture2D> _beaver_up;
        List<Texture2D> _beaver_die;
        List<Texture2D> _beaver_fly;
        List<Texture2D> _beaver_upR;
        List<Texture2D> _beaver_owl;
        List<Texture2D> _beaver_owl_glasses;

        List<Animation> _animations;

        float _timer;
        float _speed;

        public enum eAnimation
        {
            AnimationBite = 0,
            AnimationUp,
            AnimationUpR,
            AnimationDie,
            AnimationFly,
            AnimationOwl,
            AnimationOwlGlasses,
        };

        public enum eSound
        {
            eSound_Wow = 0
        }

        #region Constructors

        private AnimationManager()
        {
        }

        /// <summary>
        /// Gets the singleton instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static AnimationManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AnimationManager();
                }
                return _instance;
            }
        }

        /// <summary>
        /// Starts up the Singleton
        /// </summary>
        public void StartUp()
        {
            _beaver_bite = new List<Texture2D>();
            _beaver_up = new List<Texture2D>();
            _beaver_upR = new List<Texture2D>();
            _beaver_die = new List<Texture2D>();
            _beaver_fly = new List<Texture2D>();
            _beaver_owl = new List<Texture2D>();
            _beaver_owl_glasses = new List<Texture2D>();
            _animations = new List<Animation>();

            LoadTextures();

            _timer = 0.0f;
            _speed = 1.0f;
        }

        void LoadTextures()
        {
            for (int i = 1; i <= GameParameters.BEAVER_BITE_NB_FRAMES; i++)
            {
                string textureName = GameParameters.BEAVER_BITE_TEXTURE_NAME + "000" + i;
                _beaver_bite.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 1; i <= GameParameters.BEAVER_UP_NB_FRAMES; i++)
            {
                string textureName = GameParameters.BEAVER_UP_TEXTURE_NAME + "000" + i;
                _beaver_up.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 1; i <= GameParameters.BEAVER_UP_NB_FRAMES; i++)
            {
                string textureName = GameParameters.BEAVER_UPR_TEXTURE_NAME + "000" + i;
                _beaver_upR.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 1; i <= GameParameters.BEAVER_DIE_NB_FRAMES; i++)
            {
                string textureName = GameParameters.BEAVER_DIE_TEXTURE_NAME + "000" + i;
                _beaver_die.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 1; i <= GameParameters.BEAVER_FLY_NB_FRAMES; i++)
            {
                string textureName = GameParameters.BEAVER_FLY_TEXTURE_NAME + "000" + i;
                _beaver_fly.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 1; i <= 9; i++)
            {
                string textureName = GameParameters.BEAVER_OWL_TEXTURE_NAME + "000" + i;
                _beaver_owl.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 10; i <= GameParameters.BEAVER_OWL_NB_FRAMES; i++)
            {
                string textureName = GameParameters.BEAVER_OWL_TEXTURE_NAME + "00" + i;
                _beaver_owl.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 1; i <= 9; i++)
            {
                string textureName = GameParameters.BEAVER_OWL_GLASSES_TEXTURE_NAME + "000" + i;
                _beaver_owl_glasses.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }
            for (int i = 10; i <= GameParameters.BEAVER_OWL_GLASSES_NB_FRAMES; i++)
            {
                string textureName = GameParameters.BEAVER_OWL_GLASSES_TEXTURE_NAME + "00" + i;
                _beaver_owl_glasses.Add(RendererManager.Instance.Content.Load<Texture2D>(textureName));
            }

        }

        /// <summary>
        /// GetPosition
        /// </summary>
        /// <returns></returns>
        public void Update(GameTime gametime)
        {
            foreach (Animation anim in _animations)
            {
                if (anim.State == Animation.eState.ePlaying)
                {
                    anim.Update(gametime);
                }
            }
        }

        public Animation PlayAnimation(eAnimation eanimation)
        {
            Animation animation = new Animation();
            switch (eanimation)
            {
                case eAnimation.AnimationBite:
                    animation.Texture = _beaver_bite;
                    animation.Speed = 0.05f;
                    //animation.State = Animation.eState.ePlaying;
                    animation.MaxFrames = GameParameters.BEAVER_BITE_NB_FRAMES;
                    _animations.Add(animation);
                    break;
                case eAnimation.AnimationUp:
                    animation.Texture = _beaver_up;
                    animation.Speed = 0.05f;
                    //animation.State = Animation.eState.ePlaying;
                    animation.MaxFrames = GameParameters.BEAVER_UP_NB_FRAMES;
                    _animations.Add(animation);
                    break;
                case eAnimation.AnimationUpR:
                    animation.Texture = _beaver_upR;
                    animation.Speed = 0.05f;
                    //animation.State = Animation.eState.ePlaying;
                    animation.MaxFrames = GameParameters.BEAVER_UP_NB_FRAMES;
                    _animations.Add(animation);
                    break;
                case eAnimation.AnimationDie:
                    animation.Texture = _beaver_die;
                    animation.Speed = 0.05f;
                    //animation.State = Animation.eState.ePlaying;
                    animation.MaxFrames = GameParameters.BEAVER_DIE_NB_FRAMES;
                    _animations.Add(animation);
                    break;
                case eAnimation.AnimationFly:
                    animation.Texture = _beaver_fly;
                    animation.Speed = 0.05f;
                    //animation.State = Animation.eState.ePlaying;
                    animation.MaxFrames = GameParameters.BEAVER_FLY_NB_FRAMES;
                    _animations.Add(animation);
                    break;
                case eAnimation.AnimationOwl:
                    animation.Texture = _beaver_owl;
                    animation.Speed = 0.05f;
                    //animation.State = Animation.eState.ePlaying;
                    animation.MaxFrames = GameParameters.BEAVER_OWL_NB_FRAMES;
                    _animations.Add(animation);
                    break;
                case eAnimation.AnimationOwlGlasses:
                    animation.Texture = _beaver_owl_glasses;
                    animation.Speed = 0.05f;
                    //animation.State = Animation.eState.ePlaying;
                    animation.MaxFrames = GameParameters.BEAVER_OWL_GLASSES_NB_FRAMES;
                    _animations.Add(animation);
                    break;

            }
            return animation;
        }

        public void Draw(SpriteBatch _spriteBatch)
        {
            foreach (Animation anim in _animations)
            {
                if (anim.State == Animation.eState.ePlaying)
                {
                    anim.Draw(_spriteBatch);
                }
            }
        }

        public void Reset()
        {
            _animations.Clear();
        }

        #endregion Constructors

        #region Properties



        #endregion Properties
     
    }
}
